# -*- coding: utf-8 -*-
"""
ircvars contains variables that are used in irc strings.
There are also some regexp to match some patterns.
"""
import re, string

class _Chars:
	def __init__(self):
		pass

	def ctcp_delim(self):
		return "\x01"

	def bold(self):
		return "\x02"

	def normal(self):
		return "\x0f"

	def background(self):
		return "\x16"

	def underline(self):
		return "\x1f"

	def color(self, num):
		return "\x03%s" % num

	def aqua(self):
		return self.color("11")

	def black(self):
		return self.color("01")

	def blue(self):
		return self.color("12")

	def brown(self):
		return self.color("05")

	def dark_blue(self):
		return self.color("02")

	def dark_gray(self):
		return self.color("14")

	def dark_green(self):
		return self.color("03")

	def green(self):
		return self.color("09")

	def light_gray(self):
		return self.color("15")

	def orange(self):
		return self.color("07")

	def pink(self):
		return self.color("13")

	def purple(self):
		return self.color("06")

	def red(self):
		return self.color("04")

	def teal(self):
		return self.color("10")

	def white(self):
		return self.color("00")

	def yellow(self):
		return self.color("08")
ch = _Chars()

_abredged_colors = {
	"B": ch.bold(),
	"N": ch.normal(),
	"U": ch.underline(),
	"R": ch.background(),

	"a": ch.aqua(),
	"b": ch.black(),
	"l": ch.blue(),
	"n": ch.brown(),
	"+l": ch.dark_blue(),
	"+g": ch.dark_gray(),
	"+e": ch.dark_green(),
	"e": ch.green(),
	"g": ch.light_gray(),
	"o": ch.orange(),
	"k": ch.pink(),
	"p": ch.purple(),
	"r": ch.red(),
	"t": ch.teal(),
	"w": ch.white(),
	"y": ch.yellow()
}

#maximum length for msg (RFC 2812)
_maxlen = 410 #512
_ircmsg_re = re.compile("^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<parameters> .+))?")
_linesep_re = re.compile("\r?\n")
_source_re = re.compile("^(?P<nname>[^!]+)(!(?P<rname>[^@]+)@(?P<hname>.+))?$")
_ctcp_re = re.compile("^\x01(?P<tag>[^ ]+)(?: (?P<parameters>.*))?\x01$")

_special = "-[]\\`^{}"
_nick_characters = string.ascii_letters + string.digits + _special
_ircstring_translation = string.maketrans(string.ascii_uppercase + "[]\\^",
                                          string.ascii_lowercase + "{}|~")

numeric_events = {
	"001": "welcome",	"002": "yourhost",
	"003": "created",	"004": "myinfo",
	"005": "featurelist",

	"200": "tracelink",	"201": "traceconnecting",
	"202": "tracehandshake",	"203": "traceunknown",
	"204": "traceoperator",	"205": "traceuser",
	"206": "traceserver",	"207": "traceservice",
	"208": "tracenewtype",	"209": "traceclass",
	"210": "tracereconnect",	"211": "statslinkinfo",
	"212": "statscommands",	"213": "statscline",
	"214": "statsnline",	"215": "statsiline",
	"216": "statskline",	"217": "statsqline",
	"218": "statsyline",	"219": "endofstats",
	"221": "umodeis",	"231": "serviceinfo",
	"232": "endofservices",	"233": "service",
	"234": "servlist",	"235": "servlistend",
	"241": "statslline",	"242": "statsuptime",
	"243": "statsoline",	"244": "statshline",
	"250": "luserconns",	"251": "luserclient",
	"252": "luserop",	"253": "luserunknown",
	"254": "luserchannels",	"255": "luserme",
	"256": "adminme",	"257": "adminloc1",
	"258": "adminloc2",	"259": "adminemail",
	"261": "tracelog",	"262": "endoftrace",
	"263": "tryagain",	"265": "n_local",
	"266": "n_global",

	"300": "none",	"301": "away",
	"302": "userhost",	"303": "ison",
	"305": "unaway",	"306": "nowaway",
	"311": "whoisuser",	"312": "whoisserver",
	"313": "whoisoperator",	"314": "whowasuser",
	"315": "endofwho",	"316": "whoischanop",
	"317": "whoisidle",	"318": "endofwhois",
	"319": "whoischannels",	"321": "liststart",
	"322": "list",	"323": "listend",
	"324": "channelmodeis",	"329": "channelcreate",
	"330": "whoisaccount", #used on freenode to show account
	"331": "notopic",	"332": "currenttopic",
	"333": "topicinfo",	"341": "inviting",
	"342": "summoning",	"346": "invitelist",
	"347": "endofinvitelist",	"348": "exceptlist",
	"349": "endofexceptlist",	"351": "version",
	"352": "whoreply",	"353": "namreply",
	"361": "killdone",	"362": "closing",
	"363": "closeend",	"364": "links",
	"365": "endoflinks",	"366": "endofnames",
	"367": "banlist",	"368": "endofbanlist",
	"369": "endofwhowas",	"371": "info",
	"372": "motd",	"373": "infostart",
	"374": "endofinfo",	"375": "motdstart",
	"376": "endofmotd",	"377": "motd2",
	"381": "youreoper",	"382": "rehashing",
	"384": "myportis",	"391": "time",
	"392": "usersstart",	"393": "users",
	"394": "endofusers",	"395": "nousers",

	"401": "nosuchnick",	"402": "nosuchserver",
	"403": "nosuchchannel",	"404": "cannotsendtochan",
	"405": "toomanychannels",	"406": "wasnosuchnick",
	"407": "toomanytargets",	"409": "noorigin",
	"411": "norecipient",	"412": "notexttosend",
	"413": "notoplevel",	"414": "wildtoplevel",
	"421": "unknowncommand",	"422": "nomotd",
	"423": "noadmininfo",	"424": "fileerror",
	"431": "nonicknamegiven",	"432": "erroneusnickname",
	"433": "nicknameinuse",	"436": "nickcollision",
	"437": "unavailresource",	"441": "usernotinchannel",
	"442": "notonchannel",	"443": "useronchannel",
	"444": "nologin",	"445": "summondisabled",
	"446": "usersdisabled",	"451": "notregistered",
	"461": "needmoreparams",	"462": "alreadyregistered",
	"463": "nopermforhost",	"464": "passwdmismatch",
	"465": "yourebannedcreep",	"466": "youwillbebanned",
	"467": "keyset",	"471": "channelisfull",
	"472": "unknownmode",	"473": "inviteonlychan",
	"474": "bannedfromchan",	"475": "badchannelkey",
	"476": "badchanmask",	"477": "nochanmodes",
	"478": "banlistfull",	"481": "noprivileges",
	"482": "chanoprivsneeded",	"483": "cantkillserver",
	"484": "restricted",	"485": "uniqopprivsneeded",
	"491": "nooperhost",	"492": "noservicehost",

	"501": "umodeunknownflag",	"502": "usersdontmatch",
}

generated_events = [
    # Generated events
    "dcc_connect",
    "dcc_disconnect",
    "dccmsg",
    "disconnect",
    "ctcp",
    "ctcpreply",
]

protocol_events = [
    # IRC protocol events
    "error",
    "join",
    "kick",
    "mode",
    "part",
    "ping",
    "privmsg",
    "privnotice",
    "pubmsg",
    "pubnotice",
    "quit",
    "invite",
    "pong",
]
all_events = generated_events + protocol_events + numeric_events.values()
